package cst.neu.yuejian_31.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.bean.AddorderBean;
import cst.neu.yuejian_31.bean.UserInfoBean;
import cst.neu.yuejian_31.iface.AddorderListener;
import cst.neu.yuejian_31.iface.UserInfoListener;
import cst.neu.yuejian_31.model.AddorderModel;
import cst.neu.yuejian_31.model.UserInfoModel;

public class ClassOrderActivity extends BaseActivity implements UserInfoListener,AddorderListener {
    private TextView className,classMoney,address,userName,userTel,toTal,ordernum;
    private ImageView add,resort,classPic,btn_return;
    private Button btn_order;
    private String userId,classId,price;
    private int num,sum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class_order);
        btn_return= (ImageView) findViewById(R.id.imageView_class_order_return);
        initViews();
        initDates();
        initEvent();
    }

    private void initEvent() {
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num=Integer.parseInt(ordernum.getText().toString());
                num++;
                ordernum.setText(String.valueOf(num));
                sum=Integer.parseInt(classMoney.getText().toString())*Integer.parseInt(ordernum.getText().toString());
                toTal.setText(String.valueOf(sum));
            }
        });
        resort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num=Integer.parseInt(ordernum.getText().toString());
                if(num>1){
                    num--;
                    ordernum.setText(String.valueOf(num));
                    sum=Integer.parseInt(classMoney.getText().toString())*Integer.parseInt(ordernum.getText().toString());
                    toTal.setText(String.valueOf(sum));
                }
            }
        });
        btn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doOrder();
            }
        });

    }

    private void doOrder(){
        AddorderModel orderModel=new AddorderModel();
        orderModel.addOrder(userId,classId,String.valueOf(num),String.valueOf(sum),this);

    }

    private void initDates() {
        Intent intent=getIntent();
        classId=intent.getStringExtra("course_id");
        className.setText(intent.getStringExtra("course_name"));
        classMoney.setText(intent.getStringExtra("course_money"));
        address.setText(intent.getStringExtra("course_address"));
        toTal.setText(intent.getStringExtra("course_money"));
        Picasso.with(ClassOrderActivity.this).load(Constant.CLASS_URL+intent.getStringExtra("course_pic").toString()).into(classPic);

        userId=getUserId();
        UserInfoModel userInfoModel=new UserInfoModel();
        userInfoModel.getUserInfo(userId,this);

    }

    private void initViews() {
        className= (TextView) findViewById(R.id.textView_class_order_name);
        classMoney= (TextView) findViewById(R.id.textview_class_order_money);
        address= (TextView) findViewById(R.id.textView_class_order_address);
        classPic= (ImageView) findViewById(R.id.imageView_class_order_pic);
        userName= (TextView) findViewById(R.id.textView_class_order_username);
        userTel=(TextView)findViewById(R.id.textView_class_order_userphone);
        add= (ImageView) findViewById(R.id.imageView_order_add);
        resort= (ImageView) findViewById(R.id.imageView_order_resort);
        toTal= (TextView) findViewById(R.id.textView_class_order_total);
        ordernum= (TextView) findViewById(R.id.textView_order_num);
        btn_order= (Button) findViewById(R.id.button_doorder);
    }

    @Override
    public void onResponse(UserInfoBean user) {
        userName.setText(user.getUsername());
        userTel.setText(user.getUserphone());

    }

    @Override
    public void onSuccess(AddorderBean addorderBean) {
        Toast.makeText(this, "下单成功", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onFail(String msg) {

    }
}
